package com.puppy.uhfreader;

import UHF18.UHFReader;
import cn.hutool.http.HttpUtil;
import utils.FileUtil;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/*
 * @Author: puppy
 * @CreateDate: 2023-06-17 17:09:00
 * @FullNameClass: $path$
 * @Description:
 * @path com.puppy.uhfreader.mainNoFrame
 */
public class MainNoFramebak {
//    private String[] bauds = {"9600bps","19200bps","38400bps","57600bps","115200bps"};
//    private String[] powers={"0 dBm","1 dBm","2 dBm","3 dBm","4 dBm","5 dBm","6 dBm","7 dBm","8 dBm","9 dBm",
//            "10 dBm","11 dBm","12 dBm","13 dBm","14 dBm","15 dBm","16 dBm","17 dBm","18 dBm","19 dBm",
//            "20 dBm","21 dBm","22 dBm","23 dBm","24 dBm","25 dBm","26 dBm","27 dBm","28 dBm","29 dBm","30 dBm"};


    private static String ipaddr="192.168.75.202";
    private static final int Port = 6000;
    private static UHFReader uhf = new UHFReader();
    private static String[] EPC;
    private static String sepc;
    private static Properties properties = new Properties();
    private static ArrayList<Account> data = new ArrayList<>();
    private static String temp[][] = {{"1","1","1"},{"1","1","1"}};

    public static void main(String[] args) throws InterruptedException {
        String filePath = "src/main/resources/" ;
        try {
            properties.load(new FileInputStream("src/main/resources/config.properties"));
            uhf.OpenByTcp(properties.getProperty("ipaddr"), Integer.parseInt(properties.getProperty("port")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String cardNo = properties.getProperty("cardno");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        EPC=uhf.Inventory();
                        if(EPC != null ){
                            for(int m=0;m<EPC.length;m++  ) {
                                /**
                                 * sepc:判断为空or（不为空并且与RFID接收数据不一致）
                                 * 为空：代表第一次
                                 * 不为空并且与RFID接收数据不一致：则需要重新加载摄像，如果一致则不切换摄像头。
                                 */
                                if (sepc == null || (sepc != null && (!sepc.equals(EPC[m])))){
                                    sepc = EPC[m];
                                    System.out.println(sepc);
                                    data.clear();
                                    //TODO 在此处调用接口
                                    Map<String,Object> params = new HashMap<>();
                                    params.put("epcNo" ,sepc);
                                    params.put("Appkey" ,properties.getProperty("Appkey"));
                                    params.put("secret" ,properties.getProperty("secret"));
                                    String res = HttpUtil.get(properties.getProperty("hikurl"),params);
                                    FileUtil.writeFile(filePath, LocalDate.now().toString().concat(".txt"), sepc.concat(":").concat(res));

                                    try{
                                        Object o = properties.get(sepc);
                                        if(o != null){
                                            String accouts = properties.get(sepc).toString();
                                            String[] element = accouts.split("&");
                                            temp[0][0] = element[0].toString();
                                            temp[0][1] = element[1].toString();
                                            temp[0][2] = element[2].toString();
                                            temp[1][0] = element[3].toString();
                                            temp[1][1] = element[4].toString();
                                            temp[1][2] = element[5].toString();
                                        }

//                                        TwoCamerasWindow.run(temp);
//                                        args= null;
                                    }catch (Exception ex){
                                        ex.printStackTrace();
                                        System.out.println("卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n卡号不对！！！！！！！！！\n");
                                    }
                                }
                            }
                        }
                    }
                });
            }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runnable,0,1, TimeUnit.MILLISECONDS);


    }
    private static void Inventory()
    {

        EPC=uhf.Inventory();
        if(EPC != null )
        {
            for(int m=0;m<EPC.length;m++  )
            {
                String sepc=EPC[m];
                boolean inlist=false;
            }
        }

    }
}

